#!/usr/bin/perl

chomp($subnet = @ARGV[0] || die "Inserisci la subnet!\n");
(@ARGV[1] eq "--hide") && ($hide = 1);
(@ARGV[1] eq "--report") && ($report = 1);
$mark = (split(/\./,$subnet))[0];
if ($report eq "1") {
	open (REPORT, ">./report.html"); 
	print REPORT "<p align=\"center\"> Kl1mT's SMBScanner Report </p>";
	close REPORT;
}

@lista = `./nmbping.pl $subnet | grep $mark | cut -d \" \" -f 1 `;
foreach (@lista) {
    #print "$_\n".`./sf $_`;
    if ($report eq 1) {
	@shares = `./sfhtml $_`;
	@shares_html = @shares;
    } else {
	@shares = `./sf $_`;
    	$risultato = "--------------------\nHost $_"; 	
    }
    $noconn = 0; 
    foreach $riga(@shares) {
	$riga =~ /\tServer.*?Comment/ && last;
	$riga =~ /Il NetBIOS/ && ($risultato = $risultato.$riga);
	if (($riga =~ /Nessuna/) || ($riga =~ /session request/) || ($riga =~ /Error returning/) || ($riga =~ /failed/))  {
			$risultato = $risultato."Nessuna condivisione";
			$noconn = 1;
			last;
		}

	(($riga =~ /\t/) && !($riga =~ /Sharename/) && !($riga =~ /---------/)) && ($risultato = $risultato.$riga);
	
    }
   if (not($noconn eq 1)) {
	 if ($report eq 1) {
           open (REPORT, ">>./report.html");
            print REPORT @shares_html;
           close REPORT;
        } else {
		print "$risultato\n";
		`nautilus smb://$_`;
	}
   }
}
